package org.jfbmonitor.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.jfbmonitor.beans.Attachment;
import org.jfbmonitor.beans.Transaction;
import org.jfbmonitor.connection.FBConnection;
import org.jfbmonitor.language.Language;

public class TransactionDao {
	private Statement statement;
	private ResultSet resultSet;
	private StringBuffer listFields;
	
	public TransactionDao(){
		
		listFields = new StringBuffer();
		
		listFields.append("SELECT"); 
	 	listFields.append("  T.MON$TRANSACTION_ID,");
	 	listFields.append("  CASE T.MON$STATE");
	 	listFields.append("    WHEN 0 THEN '"+Language.getInstance().getProperties("MON$STATE.IDLE")+"'");
	 	listFields.append("    WHEN 1 THEN '"+Language.getInstance().getProperties("MON$STATE.ACTIVE")+"'");
	 	listFields.append("  END AS MON$STATE,"); 
	 	listFields.append("  T.MON$TIMESTAMP, T.MON$TOP_TRANSACTION,");
	 	listFields.append("  T.MON$OLDEST_TRANSACTION, T.MON$OLDEST_ACTIVE,");
	 	listFields.append("  CASE T.MON$ISOLATION_MODE");
	 	listFields.append("    WHEN 0 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.ISOLATION_MODE.CONSISTENCY")+"'"); 
	 	listFields.append("    WHEN 1 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.ISOLATION_MODE.CONCURRENCY")+"'");
	 	listFields.append("    WHEN 2 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.ISOLATION_MODE.READCOMMITTEDRECORDVERSION")+"'");
	 	listFields.append("    WHEN 3 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.ISOLATION_MODE.READCOMMITTEDNORECORDVERSION")+"'");
	 	listFields.append("  END AS MON$ISOLATION_MODE,");
	 	listFields.append("  CASE T.MON$LOCK_TIMEOUT");
	 	listFields.append("    WHEN -1 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.LOCK_TIMEOUT.INFINITEWAIT")+"'");
	 	listFields.append("    WHEN  0 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.LOCK_TIMEOUT.NOWAIT")+"'");
	 	listFields.append("    WHEN  1 THEN '"+Language.getInstance().getProperties("MON$TRANSACTIONS.LOCK_TIMEOUT.TIMEOUTN")+"'");
	 	listFields.append("  END MON$LOCK_TIMEOUT,");	 	
	    listFields.append("  CASE T.MON$READ_ONLY");
	 	listFields.append("    WHEN 0 THEN '"+Language.getInstance().getProperties("MON$FLAG.NO")+"'");
	 	listFields.append("    WHEN 1 THEN '"+Language.getInstance().getProperties("MON$FLAG.YES")+"'");
	 	listFields.append("  END AS MON$READ_ONLY,");	 	
	 	listFields.append("  CASE T.MON$AUTO_COMMIT");
	 	listFields.append("    WHEN 0 THEN '"+Language.getInstance().getProperties("MON$FLAG.NO")+"'");
	 	listFields.append("    WHEN 1 THEN '"+Language.getInstance().getProperties("MON$FLAG.YES")+"'");
	 	listFields.append("  END AS MON$AUTO_COMMIT,");	 	
	 	listFields.append("  CASE T.MON$AUTO_UNDO");
	 	listFields.append("    WHEN 0 THEN '"+Language.getInstance().getProperties("MON$FLAG.NO")+"'");
	 	listFields.append("    WHEN 1 THEN '"+Language.getInstance().getProperties("MON$FLAG.YES")+"'");
	 	listFields.append("  END AS MON$AUTO_UNDO,");	 	
	 	listFields.append("  T.MON$STAT_ID, A.MON$ATTACHMENT_ID,");
	 	listFields.append("  A.MON$SERVER_PID,");
	 	listFields.append("  CASE A.MON$STATE");
	 	listFields.append("    WHEN 0 THEN '"+Language.getInstance().getProperties("MON$STATE.IDLE")+"'");
	 	listFields.append("    WHEN 1 THEN '"+Language.getInstance().getProperties("MON$STATE.ACTIVE")+"'");	 	
	 	listFields.append("  END AS MON$STATE,");
	 	listFields.append("  A.MON$ATTACHMENT_NAME, A.MON$USER, A.MON$ROLE,");
	 	listFields.append("  A.MON$REMOTE_PROTOCOL, A.MON$REMOTE_ADDRESS,");
	 	listFields.append("  A.MON$REMOTE_PID, C.RDB$CHARACTER_SET_NAME,");
	 	listFields.append("  A.MON$TIMESTAMP, A.MON$GARBAGE_COLLECTION,");
	 	listFields.append("  A.MON$REMOTE_PROCESS, A.MON$STAT_ID ");
	 	listFields.append("FROM MON$TRANSACTIONS T");
	 	listFields.append("  LEFT JOIN MON$ATTACHMENTS    A ON A.MON$ATTACHMENT_ID = T.MON$ATTACHMENT_ID");
	 	listFields.append("  LEFT JOIN RDB$CHARACTER_SETS C ON A.MON$CHARACTER_SET_ID = C.RDB$CHARACTER_SET_ID");
		
	}
	
	public List<Transaction> getAll(String condition) throws SQLException {

        if (condition.trim()!="") {
			listFields.append(" where "+condition);
		}
	 	statement = FBConnection.getInstance().getConnection().createStatement();
	 	//System.out.println(listFields.toString());
	 	
		resultSet = statement.executeQuery(listFields.toString());
		List<Transaction> list = new ArrayList<Transaction>();
		while (resultSet.next()){
			Attachment attachment = new Attachment( 
					resultSet.getInt("MON$ATTACHMENT_ID"),
					resultSet.getInt("MON$SERVER_PID"),					  
					resultSet.getString("MON$STATE"),
					resultSet.getString("MON$ATTACHMENT_NAME"),
					resultSet.getString("MON$USER"),					  
					resultSet.getString("MON$ROLE"),					  
					resultSet.getString("MON$REMOTE_PROTOCOL"),					  
					resultSet.getString("MON$REMOTE_ADDRESS"),					  
					resultSet.getInt("MON$REMOTE_PID"),
					resultSet.getString("MON$REMOTE_PROCESS"),					  
					resultSet.getString("RDB$CHARACTER_SET_NAME"),
					resultSet.getDate("MON$TIMESTAMP"),
					resultSet.getShort("MON$GARBAGE_COLLECTION"),
					resultSet.getInt("MON$STAT_ID")
					);
			list.add(new Transaction(
					resultSet.getInt("MON$TRANSACTION_ID"),	attachment,
					resultSet.getString("MON$STATE"),
					resultSet.getDate("MON$TIMESTAMP"),
					resultSet.getInt("MON$TOP_TRANSACTION"),
					resultSet.getInt("MON$OLDEST_TRANSACTION"),
					resultSet.getInt("MON$OLDEST_ACTIVE"),					
					resultSet.getString("MON$ISOLATION_MODE"),
					resultSet.getString("MON$LOCK_TIMEOUT"),
					resultSet.getString("MON$READ_ONLY"),
					resultSet.getString("MON$AUTO_COMMIT"),
					resultSet.getString("MON$AUTO_UNDO"),
					resultSet.getInt("MON$STAT_ID")
					)
			);			
		}			
		resultSet.getStatement().close();	
		return list;
	}
}
